#! /usr/bin/python2.4

# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""check_svn_revprops.py unittest"""


import unittest

import gvn.hooks.info
import gvn.hooks.pre_revprop_change.check_svn_revprops

import testing.hooks.common

SUPER_USERS = "svn-team"

class TestSvnRevPropsChange(testing.hooks.common.HookTestCase):

  def setUp(self):
    testing.hooks.common.HookTestCase.setUp(self)

    # create group pickle to use as our group file
    user_groups= {
      "user1": [ "group1" ],
      "adminuser": [ SUPER_USERS ],
    }

    for user in user_groups:
      self.userdb.WriteUserGroups(user, user_groups[user])

  def _ChangeProps(self, user, prop_name, action):
    """Return RunHook result triple from txn based on arguments.

    Args:
      user: who wants to set/change/delete the property
      prop_name: which property to create
      action: A(dd)|D(elete)|M(odify)

    Returns:
      0 (pass), 1 (fail), string (fail and display string)

    Make a txn by setting the property to path and check if author can submit it
    with its perms, or the perms from approve_user
    """

    # seed argv with expected arguments during a regular call, and overwrite
    # the ones we need to, with our own values lower down
    argv = ["pre-revprop-change", "repo_name_bogus_ignored", 1,
                                                      user, prop_name, action]
    hi = gvn.hooks.info.HookInfo(argv, userdb=self.userdb)
    hi._super_users = SUPER_USERS
    hi._revision = 1

    ret = gvn.hooks.pre_revprop_change.check_svn_revprops.RunHook(hi,
                                                                  self._logger)
    return ret


  def testPropSet(self):
    """A random user can set a random property
    """
    self.assertHookResult(self._ChangeProps("user1", "randomprop", "A"), 0)

  def testAdminUserPropSet(self):
    """An admin user can set a random property
    """
    self.assertHookResult(self._ChangeProps("adminuser", "randomprop", "A"), 0)

  def testSvnPropSet(self):
    """A random user cannot set an svn:x property
    """
    self.assertHookResult(self._ChangeProps("user1", "svn:foo", "A"),
                "user1 is not allowed to add property svn:foo for rev 1")

  def testAdminUserSvnPropSet(self):
    """An admin user cannot set an svn:x property
    """
    self.assertHookResult(self._ChangeProps("adminuser", "svn:foo", "A"),
                "adminuser is not allowed to add property svn:foo for rev 1")


if __name__ == "__main__":
  unittest.main()
